using System.Reflection;
using Core.Db.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;

namespace Core.Db;

public class AppDbContext : IdentityDbContext<IdentityUser> //DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }

    public DbSet<TouristRoute> TouristRoutes { get; set; }
    public DbSet<TouristRoutePicture> TouristRoutePictures { get; set; }
    public DbSet<ShoppingCart> ShoppingCarts { get; set; }
    public DbSet<LineItem> LineItems { get; set; }
    public DbSet<Order> Orders { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        var touristRouteJsonData = File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/Database/touristRoutesMockData.json");
        IList<TouristRoute> touristRoutes = JsonConvert.DeserializeObject<IList<TouristRoute>>(touristRouteJsonData);
        modelBuilder.Entity<TouristRoute>().HasData(touristRoutes);

        var touristRoutePicturesJsonData = File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/Database/touristRoutePicturesMockData.json");
        IList<TouristRoutePicture> touristRoutePictures = JsonConvert.DeserializeObject<IList<TouristRoutePicture>>(touristRoutePicturesJsonData);
        modelBuilder.Entity<TouristRoutePicture>().HasData(touristRoutePictures);


        //初始化用户与角色的种子数据
        //1.更新用户与角色的外键
        modelBuilder.Entity<ApplicationUser>(u =>
        {
            u.HasMany(x => x.UserRoles)
                .WithOne().HasForeignKey(ur => ur.UserId).IsRequired();
        });
        //2.添加管理员角色
        var adminRoleId = "3fa85f64-5717-4562-b3fc-2c963f66afa6";
        modelBuilder.Entity<IdentityRole>().HasData(
            new IdentityRole()
            {
                Id = adminRoleId,
                Name = "Admin",
                NormalizedName = "Admin".ToUpper()
            });

        //3.添加用户
        var adminUserId = "3fa85f64-5717-4562-b3fc-2c963f66afa7";
        ApplicationUser adminUser = new ApplicationUser
        {
            Id = adminUserId,
            UserName = "admin@us.com",
            NormalizedUserName = "admin@us.com".ToUpper(),
            Email = "admin@us.com",
            NormalizedEmail = "admin@us.com".ToUpper(),
            TwoFactorEnabled = false,
            EmailConfirmed = true,
            PhoneNumber = "123456789",
            PhoneNumberConfirmed = false
        };
        var ph = new PasswordHasher<ApplicationUser>();
        adminUser.PasswordHash = ph.HashPassword(adminUser, "Us@123");
        modelBuilder.Entity<ApplicationUser>().HasData(adminUser);
        //4.给用户加入管理员角色
        modelBuilder.Entity<IdentityUserRole<string>>().HasData(
            new IdentityUserRole<string>()
            {
                RoleId = adminRoleId,
                UserId = adminUserId,
            });
    }
}